package com.jf.cloud.platform.service.impl;

import com.jf.cloud.api.delivery.feign.AreaFeignClient;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageAdapter;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.platform.dto.InformationDTO;
import com.jf.cloud.platform.mapper.InfoCateMapper;
import com.jf.cloud.platform.mapper.InformationMapper;
import com.jf.cloud.platform.model.Information;
import com.jf.cloud.platform.service.InformationService;
import com.jf.cloud.platform.vo.InformationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 
 *
 * @author zz
 * @date 2023-03-06 09:14:00
 */
@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;

    @Autowired
    private InfoCateMapper infoCateMapper;

    @Autowired
    private AreaFeignClient areaFeignClient;

    @Override
    public PageVO<Information> page(PageDTO pageDTO,InformationDTO informationDTO) {
        return PageUtil.doPage(pageDTO, () -> informationMapper.list(informationDTO));
    }

    @Override
    public PageVO<InformationVO> pageList(PageDTO pageDTO, InformationDTO informationDTO) {
        PageVO<InformationVO> pageVO = new PageVO<>();
        // 用户信息
        Long userTotal = informationMapper.countPageByParam(informationDTO);
        pageVO.setTotal(userTotal);
        pageVO.setPages(PageUtil.getPages(pageVO.getTotal(), pageDTO.getPageSize()));
        if (Objects.equals(0L, userTotal)) {
            pageVO.setList(Collections.emptyList());
            return pageVO;
        }
        List<InformationVO> userList = getInformationList(informationDTO,pageDTO);
        userList.stream().forEach(e->e.setCateName(infoCateMapper.getById(e.getCateId().longValue()).getCateName()));
        userList.stream().forEach(e->e.setAreaCode(infoCateMapper.getById(e.getCateId().longValue()).getAreaCode()));
        userList.stream().forEach(e->e.setAreaName(areaFeignClient.getArea(e.getAreaCode()).getData().getAreaName()));
        pageVO.setList(userList);
        return pageVO;
    }

    @Override
    public List<InformationVO> getInformationList(InformationDTO informationDTO,PageDTO pageDTO) {
        return informationMapper.getInformationList(informationDTO,new PageAdapter(pageDTO));
    }


    @Override
    public Information getById(Long id) {
        return informationMapper.getById(id);
    }

    @Override
    public void save(Information information) {
        informationMapper.save(information);
    }

    @Override
    public void update(Information information) {
        informationMapper.update(information);
    }

    @Override
    public void deleteById(Long id) {
        informationMapper.deleteById(id);
    }
}
